#   Copyright (C) 1993-2015 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.

# Please email any bugs, comments, and/or additions to this file to:
# bug-dejagnu@prep.ai.mit.edu

# This file was written by Rob Savoye <rob@cygnus.com>
# and rewritten by Ian Lance Taylor <ian@cygnus.com>

if ![is_remote host] {
    if {[which $OBJDUMP] == 0} then {
	perror "$OBJDUMP does not exist"
	return
    }
}

send_user "Version [binutil_version $OBJDUMP]"

# Simple test of objdump -i

set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -i"]

set cpus_expected [list]
lappend cpus_expected aarch64 alpha arc HS arm cris
lappend cpus_expected d10v d30v fr30 fr500 fr550 h8 hppa i386 i860 i960 iamcu ip2022
lappend cpus_expected m16c m32c m32r m68hc11 m68hc12 m68k m88k MCore mep c5 h1 MicroBlaze
lappend cpus_expected mips mn10200 mn10300 ms1 msp MSP430 nds32 n1h_v3 ns32k
lappend cpus_expected or1k or1knd pj powerpc pyramid romp rs6000 s390 sh sparc
lappend cpus_expected tahoe tic54x tic80 tilegx tms320c30 tms320c4x tms320c54x
lappend cpus_expected v850 vax we32k x86-64 xscale xtensa z8k z8001 z8002

# Make sure the target CPU shows up in the list.
lappend cpus_expected ${target_cpu}

# Create regexp
set cpus_regex "([join $cpus_expected | ])"

verbose -log "CPU regex: $cpus_regex"

set want "BFD header file version.*srec\[^\n\]*\n\[^\n\]*header \[^\n\]*endian\[^\n\]*, data \[^\n\]*endian.*$cpus_regex"

if [regexp $want $got] then {
    pass "objdump -i"
} else {
    fail "objdump -i"
}

# The remaining tests require a test file.


if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
    return
}
if [is_remote host] {
    set testfile [remote_download host tmpdir/bintest.o]
} else {
    set testfile tmpdir/bintest.o
}

# Test objdump -f

set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -f $testfile"]

set want "$testfile:\[ 	\]*file format.*architecture:\[ 	\]*${cpus_regex}.*HAS_RELOC.*HAS_SYMS"

if ![regexp $want $got] then {
    fail "objdump -f"
} else {
    pass "objdump -f"
}

# Test objdump -h

set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -h $testfile"]

set want "$testfile:\[ 	\]*file format.*Sections.*\[0-9\]+\[ 	\]+\[^ 	\]*(text|TEXT|\\\$CODE\\\$)\[^ 	\]*\[ 	\]*(\[0-9a-fA-F\]+).*\[0-9\]+\[ 	\]+\[^ 	\]*(\\.data|DATA)\[^ 	\]*\[ 	\]*(\[0-9a-fA-F\]+)"

if ![regexp $want $got all text_name text_size data_name data_size] then {
    fail "objdump -h"
} else {
    verbose "text name is $text_name size is $text_size"
    verbose "data name is $data_name size is $data_size"
    set ets 8
    set eds 4
    # The [ti]c4x target has the property sizeof(char)=sizeof(long)=1
    if [istarget *c4x*-*-*] then {
        set ets 2
        set eds 1
    }
    # c54x section sizes are in bytes, not octets; adjust accordingly
    if [istarget *c54x*-*-*] then {
	set ets 4
	set eds 2
    }
    if {[expr "0x$text_size"] < $ets || [expr "0x$data_size"] < $eds} then {
	send_log "sizes too small\n"
	fail "objdump -h"
    } else {
	pass "objdump -h"
    }
}

# Test objdump -t

set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -t $testfile"]

if [info exists vars] then { unset vars }
while {[regexp "(\[a-z\]*_symbol)(.*)" $got all symbol rest]} {
    set vars($symbol) 1
    set got $rest
}

if {![info exists vars(text_symbol)] \
     || ![info exists vars(data_symbol)] \
     || ![info exists vars(common_symbol)] \
     || ![info exists vars(external_symbol)]} then {
    fail "objdump -t"
} else {
    pass "objdump -t"
}

# Test objdump -r

set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -r $testfile"]

set want "$testfile:\[ 	\]*file format.*RELOCATION RECORDS FOR \\\[\[^\]\]*(text|TEXT|\\\$CODE\\\$)\[^\]\]*\\\].*external_symbol"

if [regexp $want $got] then {
    pass "objdump -r"
} else {
    fail "objdump -r"
}

# Test objdump -s

set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -s $testfile"]

set want "$testfile:\[ 	\]*file format.*Contents.*(text|TEXT|\\\$CODE\\\$)\[^0-9\]*\[ 	\]*\[0-9a-fA-F\]*\[ 	\]*(00000001|01000000|00000100).*Contents.*(data|DATA)\[^0-9\]*\[ 	\]*\[0-9a-fA-F\]*\[ 	\]*(00000002|02000000|00000200)"

if [regexp $want $got] then {
    pass "objdump -s"
} else {
    fail "objdump -s"
}

# Test objdump -s on a file that contains a compressed .debug section

if { ![is_elf_format] } then {
    unsupported "objdump compressed debug"
} elseif { ![binutils_assemble $srcdir/$subdir/dw2-compressed.S tmpdir/dw2-compressed.o] } then {
    fail "objdump compressed debug"
} else {
    if [is_remote host] {
	set compressed_testfile [remote_download host tmpdir/dw2-compressed.o]
    } else {
	set compressed_testfile tmpdir/dw2-compressed.o
    }

    set got [remote_exec host "$OBJDUMP $OBJDUMPFLAGS -s -j .zdebug_abbrev $compressed_testfile" "" "/dev/null" "objdump.out"]

    if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
	fail "objdump -s -j .zdebug_abbrev (reason: unexpected output)"
	send_log $got
	send_log "\n"
    }

    if { [regexp_diff objdump.out $srcdir/$subdir/objdump.s] } then {
	fail "objdump -s -j .zdebug_abbrev"
    } else {
	pass "objdump -s -j .zdebug_abbrev"
    }

    # Test objdump -W on a file that contains some compressed .debug sections

    set got [remote_exec host "$OBJDUMP $OBJDUMPFLAGS -W $compressed_testfile" "" "/dev/null" "objdump.out"]

    if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
	fail "objdump -W (reason: unexpected output)"
	send_log $got
	send_log "\n"
    }

    if { [regexp_diff objdump.out $srcdir/$subdir/objdump.W] } then {
	fail "objdump -W"
    } else {
	pass "objdump -W"
    }
}

# Test objdump -WL on a file that contains line information for multiple files and search directories.
# Not supported on mcore and moxie targets because they do not (yet) support the generation
# of DWARF2 line debug information.

if { ![is_elf_format]
     || [istarget "hppa64*-*-hpux*"]
     || [istarget "i370-*-*"]
     || [istarget "i960-*-*"]
     || [istarget "ia64*-*-*"]
     || [istarget "mcore-*-*"]
     || [istarget "moxie-*-*"]
} then {
    unsupported "objump decode line"
} else {
    if { [istarget "or1k*-*-*"] } then {
        set decodedline_testsrc $srcdir/$subdir/dw2-decodedline-1.S
    } else {
        set decodedline_testsrc $srcdir/$subdir/dw2-decodedline.S
    }
    if { ![binutils_assemble $decodedline_testsrc tmpdir/dw2-decodedline.o] } then {
	fail "objdump decoded line"
    }

    if [is_remote host] {
	set decodedline_testfile [remote_download host tmpdir/dw2-decodedline.o]
    } else {
	set decodedline_testfile tmpdir/dw2-decodedline.o
    }

    set got [remote_exec host "$OBJDUMP $OBJDUMPFLAGS -WL $decodedline_testfile" "" "/dev/null" "objdump.out"]

    if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
	fail "objdump -WL (reason: unexpected output)"
	send_log $got
	send_log "\n"
    }

    if { [regexp_diff objdump.out $srcdir/$subdir/objdump.WL] } then {
	fail "objdump -WL"
    } else {
	pass "objdump -WL"
    }
}

# Options which are not tested: -a -d -D -R -T -x -l --stabs
# I don't see any generic way to test any of these other than -a.
# Tests could be written for specific targets, and that should be done
# if specific problems are found.
